package com.atguigu.tingshu.album.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
@ConfigurationProperties(prefix="minio") //读取节点
@Data
public class MinioConstantProperties implements InitializingBean {
    public static String ENDPOINT_URL;
    public static String ACCESS_KEY;
    public static String SECRET_KEY;
    public static String BUCKET_NAME;

    private String endpointUrl;
    private String accessKey;
    private String secretKey;
    private String bucketName;
    //利用SpringBean的生命周期方法， 再容器初始化当前组件对象 设置属性值以后
    // 将成员属性值 赋值给静态属性

    //容器对象属性值设置以后 调用
    @Override
    public void afterPropertiesSet() throws Exception {
        ENDPOINT_URL = this.endpointUrl;
        ACCESS_KEY = this.accessKey;
        SECRET_KEY = this.secretKey;
        BUCKET_NAME = this.bucketName;

    }
//    @Lazy //懒加载： 表示当前组件对象 在使用时才初始化
    @ConditionalOnBean(value= MinioConstantProperties.class)
    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .endpoint(MinioConstantProperties
                        .ENDPOINT_URL)
                .credentials(MinioConstantProperties.ACCESS_KEY,
                        MinioConstantProperties.SECRET_KEY)
                .build();
    }

}
